 package com.ling.ling_push;

 import android.Manifest;
 import android.os.Bundle;
 import android.util.Log;
 import android.view.SurfaceView;
 import android.view.View;
 import android.widget.Button;

 import androidx.appcompat.app.AppCompatActivity;

 import com.ling.ling_push.pusher.LivePusher;

 import pub.devrel.easypermissions.EasyPermissions;

 public class MainActivity extends AppCompatActivity {
     static MainActivity mainActivity;
     static String URL ;
     private LivePusher livePusher;
     String[] perms = {Manifest.permission.CAMERA,
             Manifest.permission.RECORD_AUDIO};
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         URL=getIntent().getStringExtra("URL");
         getSupportActionBar().hide();
         setContentView(R.layout.activity_main);

         SurfaceView surfaceView = findViewById(R.id.surfaceView);

         if (!EasyPermissions.hasPermissions(this, perms)) {
             EasyPermissions.requestPermissions(this, "直播功能需要摄像头权限和麦克风权限", 0, perms);
         }
         //相机图像的预览
         livePusher = new LivePusher(surfaceView.getHolder());
         mainActivity=this;
     }

     public void bt(View view) {
         switch (view.getId()) {
             case R.id.btn_chang_camer:
                 livePusher.switchCamera();
                 break;
             default:
                 runOnUiThread(new Runnable() {
                     @Override
                     public void run() {

                     }
                 });
                 break;
         }
     }

     /**
      * 开始直播
      *
      * @param
      */
     public void mStartLive(View view) {
         Button btn = (Button) view;
         if (btn.getText().equals("开始直播")) {
             Log.e("MainActivity","URL"+URL);
             livePusher.startPush(URL);
             btn.setText("停止直播");
         } else {
             livePusher.stopPush();
             btn.setText("开始直播");
         }

     }
     public static MainActivity GetActivity( ){
         return mainActivity;
     }
     @Override
     public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
         super.onRequestPermissionsResult(requestCode, permissions, grantResults);
         EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
     }

     @Override
     protected void onDestroy() {
         super.onDestroy();
         livePusher.release();
     }
 }